package com.link.base.wechat.wxplatform.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.link.base.tencent.corpwx.appchat.model.AppChatUser;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.model.BasicModel;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.ServiceException;
import com.link.base.wechat.wxplatform.model.WxPlatform;
import com.link.base.wechat.wxplatform.service.WxPlatformService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信公众号管理
 *
 * @author 欧阳振强
 * @date 2019/02/27 14:51
 */
@Controller
@RequestMapping("/link/wxPlatform")
public class WxPlatformController extends BasicController<WxPlatform> {

    @Resource
    private WxPlatformService wxPlatformService;

    @Override
    public BasicService<WxPlatform> getBasicService() throws Exception {
        return wxPlatformService;
    }

    /**
     * 维护公众平台 -- 该接口针对开发者模式下，用户手工维护公众号
     *
     * @author ouyangzhenqiang
     * @date 2019/03/07 16:34
     */
    @RequestMapping(value = {"/addWxPlatformFroDevMode"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addWxPlatformFroDevMode(WxPlatform record, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            wxPlatformService.addWxPlatformFroDevMode(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 更新公众号有效无效字段
     *
     * @author ouyangzhenqiang
     * @date 2019/02/28 09:54
     * @param record 公众号对象
     */
    @RequestMapping(value = {"/activeWxpp", "/inactiveWxpp"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setValidFlagWxpp(WxPlatform record, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        Long rowId = record.getId();
        if (rowId != null) {
            throw new ServiceException("WXPLAT-001");
        }
        try {
            wxPlatformService.validFlagWxppUpdate(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 设置主要公众号
     *
     * @author ouyangzhenqiang
     * @date 2019/02/28 09:56
     * @param record 公众号对象
     */
    @RequestMapping(value = {"/defaultFlagWxpp"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> defaultFlagWxpp(WxPlatform record, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        Long rowId = record.getId();
        if (rowId != null) {
            throw new ServiceException("WXPLAT-001");
        }
        try {
            wxPlatformService.defaultFlagWxppUpdate(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 将所有公众号信息更新到redis
     *
     * @author ouyangzhenqiang
     * @date 2019/02/28 09:59
     */
    @RequestMapping(value = {"/clearWxppCatch"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> clearWxppCatch(HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            wxPlatformService.loadAllWxPlatformsToRedis();
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询所有的授权标识为Y的公众号记录，
     *
     * @author ouyangzhenqiang
     * @date 2019/02/28 10:00
     * @param record 公众号对象
     */
    @RequestMapping(value = {"/queryAuthPage"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryAuthPage(@JsonParam WxPlatform record, @JsonParam QueryParams qps, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            WxPlatform t = BasicModel.transformClass(WxPlatform.class, qps);
            List<WxPlatform> list = wxPlatformService.queryAuthPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false); 
            result.put("rows", new ArrayList<AppChatUser>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询所有的公众号记录
     *
     * @author ouyangzhenqiang
     * @date 2019/02/28 10:52
     * @param entity 公众号对象
     */
    @RequestMapping(value = "/queryWxAccntName", method = RequestMethod.POST)
    @ResponseBody
    public Object queryWxAccntName(WxPlatform entity,
                                   QueryParams qps, HttpSession session, HttpServletRequest request,
                                   HttpServletResponse response) {
        List<WxPlatform> list = null;
        try {
            list = wxPlatformService.queryAll(entity);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return list;
    }

    /**
     * 查询默认的微信公众号
     *
     * @author ouyangzhenqiang
     * @date 2019/02/28 13:49
     * @param entity 公众号对象
     */
    @RequestMapping(value = "/queryDefaultByCorpid", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryDefaultByCorpid(WxPlatform entity,
                                                    QueryParams qps, HttpSession session, HttpServletRequest request,
                                                    HttpServletResponse response) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            WxPlatform wxPlatform = wxPlatformService.queryDefaultByCorpid(entity);
            result.put("result", wxPlatform);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
